// pages/secondDetail/secondDetail.js
var app = getApp();
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    id: '',
    detailInfo:'',
    showModal: false,
    modaltitle: '留言',
    reply: '',
    otherSideId: '',
    isadd: true, 
    parentId:'',
    userId: '',
    loadSuccess: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading()
    this.setData({
      userId: JSON.parse(wx.getStorageSync('userInfo')).id,
      id: options.id
    })
    this.getInfoById();
    this.getLeaveMes();
    this.addBrowse()
  },
  addBrowse: function(){
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'secondHandInfo/addBrowseNum',
      data: {
        id: _this.data.id
      },
      method:'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if (res.data.success) {
          
        }
      }
    })
  },
  getInfoById: function(){
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'secondHandInfo/getSecondHandInfo',
      data: {
        userId: _this.data.userId,
        id: _this.data.id
      },
      success: function (res) {
        _this.setData({
          loadSuccess: true
        })
        wx.hideLoading()
        if (res.data.success) {
          if (res.data.data.backupA) {
            res.data.data.backupA = res.data.data.backupA.split(",");
          }
          if (res.data.data.createTime) {
            res.data.data.createTime = util.getDays(res.data.data.createTime)
          }
         _this.setData({
           detailInfo: res.data.data
         })
        }
      }
    })
  },
  // 收藏
  collect: function (e) {
    var coll = e.currentTarget.dataset.coll;
    var _this = this;
    if (coll == 1) {
      wx.request({
        url: app.globalData.httpApiCar + 'tGyCollection/deleteCollectionRecordByParams',
        data: {
          sourceId: _this.data.id,
          userId: _this.data.userId,
          block: 4
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function (res) {
          if (res.data.success) {
            _this.setData({
              ['detailInfo.isCollection']: 0
            })
          }
          else {
            wx.showToast({
              title: res.data.error,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
    else {
      wx.request({
        url: app.globalData.httpApiCar + 'tGyCollection/save',
        data: {
          sourceId: _this.data.id,
          userId: _this.data.userId,
          block: 4
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function (res) {
          if (res.data.success) {
            wx.showToast({
              title: '收藏成功',
              icon: 'success',
              duration: 2000
            })
            _this.setData({
              ['detailInfo.isCollection']: 1
            })
          }
          else {
            wx.showToast({
              title: res.data.error,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
  },
  modalInputFunc: function(e){
    this.setData({
      reply: e.detail.value
    })
  },
  // 点赞
  likeFunc: function (e) {
    var coll = e.currentTarget.dataset.like;
    var _this = this;
    if (coll == 1) {
      wx.request({
        url: app.globalData.httpApiCar + 'tGyLike/deleteLikeRecordByParams',
        data: {
          sourceId: _this.data.id,
          userId: _this.data.userId,
          block: 4
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function (res) {
          if (res.data.success) {
            _this.setData({
              ['detailInfo.isLike']: 0
            })
            _this.getInfoById()
          }
          else {
            wx.showToast({
              title: res.data.error,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
    else {
      wx.request({
        url: app.globalData.httpApiCar + 'tGyLike/save',
        data: {
          sourceId: _this.data.id,
          userId: _this.data.userId,
          block: 4
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function (res) {
          if (res.data.success) {
            // wx.showToast({
            //   title: '点赞成功',
            //   icon: 'success',
            //   duration: 2000
            // })
            _this.setData({
              ['detailInfo.isLike']: 1
            })
            _this.getInfoById()
          }
          else {
            wx.showToast({
              title: res.data.error,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
  },
  prevent: function(){
  },
  addLeaveMes(e) {//留言发送按钮点击事件
    if (this.data.reply != '') {
      var formData = {
        userId: this.data.userId,
        infoId: this.data.id,
        message: this.data.reply,
        otherSideId: this.data.otherSideId,
        status: 8
      };
      if (this.data.isadd == 'true') {
        formData.type = 1;
      } else {
        formData.type = 2;
        formData.parentId = this.data.parentId;
      }
      var that = this;
      wx.request({
        url: app.globalData.httpApiCar + 'messageInfo/save', //仅为示例，并非真实的接口地址
        data: formData,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
          // 'content-type': 'multipart/form-data' // 默认值
        },
        success: function (res) {
          if (res.data.success) {
            that.setData({
              page: 1,
              reply: '',
              isadd: true
            })
            that.getLeaveMes(true);
            that.getInfoById()
          }
          else {
            wx.showToast({
              title: res.data.error,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
  },
  message: function(e){
    this.setData({
      otherSideId: e.currentTarget.dataset.id,
      isadd: e.currentTarget.dataset.isadd,
      parentId: e.currentTarget.dataset.itemid,
      showModal: true,
    })
  },
  confirm: function(e){
    if (this.data.reply != ''){
      this.addLeaveMes(e);
      this.cancel();
    }
    else{
      wx.showToast({
        title: '请输入回复内容',
        icon: 'none',
        duration: 2000
      })
    }
  },
  cancel: function(){
    this.setData({
      showModal: false
    })
  },
  getLeaveMes: function() {//获取留言数据
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'messageInfo/all', //仅为示例，并非真实的接口地址
      data: {
        type: 1,
        infoId: _this.data.id,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var obj = {};
        res.data.data.forEach(function (ele) {
          ele.time = '';
          if (ele.createTime) {
            // ele.time = util.getHours(new Date(ele.create_date.replace(/-/g, "/")).getTime());
            ele.time = util.getHours(ele.createTime);
          }
          if (ele.messageInfos.length > 0) {
            ele.messageInfos = ele.messageInfos.reverse();
            ele.messageInfos.forEach(function (item) {
              if (item.createTime) {
                item.time = util.getHours(item.createTime);
              }
            })

          }
        })
        _this.setData({
          replyList: res.data.data
        })
      }
    })
  },
  // 打电话
  toggleSelect: function (e) {
    var show = e.currentTarget.dataset.value == "true" ? true : false;
    this.setData({
      showSelect: show,
      //current_tel: e.currentTarget.dataset.tel
    })
  },
  callPhone: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.value
    })
    this.setData({
      showSelect: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})